<?php

namespace App\Model\Content;

use App\Model\CrudModel;

/**
 * @property int $id
 * @property int $group_id
 * @property int $sort
 * @property int $show_status
 * @property string $name
 * @property string $cover
 * @property int $jump_method
 * @property string $jump_url
 */
class AdModel extends CrudModel
{
    // 配置
    protected ?string $table = 'ad';

    protected array $adminPageListField = [
        'id', 'group_id', 'sort', 'name', 'show_status', 'cover', 'jump_method', 'jump_url',
    ];

    public function addOne(array $data)
    {
        $add = [
            'group_id' => intval($data['group_id']),
            'sort' => intval($data['sort'] ?? 0),
            'show_status' => intval($data['show_status'] ?? 0),
            'name' => trim($data['name'] ?? ''),
            'cover' => trim($data['cover']),
            'jump_method' => intval($data['jump_method']),
            'jump_url' => trim($data['jump_url'] ?? ''),
        ];
        return $this->normalAddOne($add);
    }

    public function updateById($id, array $data)
    {
        return $this->buildQuery(['id' => $id,])->update([
            'sort' => intval($data['sort'] ?? 0),
            'show_status' => intval($data['show_status'] ?? 0),
            'name' => trim($data['name'] ?? ''),
            'cover' => trim($data['cover']),
            'jump_method' => intval($data['jump_method']),
            'jump_url' => trim($data['jump_url'] ?? ''),
            'updated_at' => time(),
        ]);
    }

    protected function getAllSort($builder)
    {
        return $builder->orderByDesc('sort');
    }
}